package com.utils;

import java.io.File;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.conn.ConnectTimeoutException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.widget.Toast;

import com.httputils.CloseRequestException;
import com.httputils.ConnMachine;
import com.httputils.NetworkConstant;
import com.httputils.TransactionValues;
import com.myfp.myfund.AppBean;
import com.myfp.myfund.ICallbackListener;

/**
 * 功能：联网请求工具类
 * @author User
 *
 */
public class MyNetwork {

	private AppBean appBean;
	private Context mCt;
	private ProgressDialog waitDialog;
	private String title, message;

	public MyNetwork(AppBean appB, Context ct) {
		appBean = appB;
		mCt = ct;
		title = "请稍等！";
		message = "正在获取数据...";
	}

	public void setTitleMessage(String title, String message) {
		this.title = title;
		this.message = message;
	}

	/**
	 * 发送POST请求
	 * 
	 * @param paramList
	 *            请求参数
	 * @param ct
	 *            上下文对象
	 * @param url
	 *            请求地址
	 * @param waitDialogType
	 *            请求等待对话框类型 DialogUtils.NO_WAIT_DIALOG 不需要等待框
	 *            DialogUtils.WAIT_DIALOG 普通的等待框
	 */
	public void sendPostRequest(List<NameValuePair> paramList, Context ct,
			String url, int waitDialogType) {
		LogUtils.print(ct, LogUtils.TYPE_CONSOLE, "saf",
				"网络状态：" + appBean.isNetStat() + "");

		if (appBean.isNetStat()) {// 网络状态正常
			if (!appBean.hs.containsKey(url)) {
				BaseThread thread = new BaseThread(paramList, ct, url,
						waitDialogType);
				thread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);
		}
	}

	/**
	 * post请求----上送参数是map
	 * 
	 * @param map
	 * @param ct
	 * @param url
	 * @param waitDialogType
	 */
	public void sendJsonRequest(Map<String, Object> map, Context ct,
			String url, int waitDialogType) {
		LogUtils.print(ct, LogUtils.TYPE_CONSOLE, "saf",
				"网络状态：" + appBean.isNetStat() + "");

		if (appBean.isNetStat()) {// 网络状态正常
			if (!appBean.hs.containsKey(url)) {
				BaseJsonThread thread = new BaseJsonThread(map, ct, url,
						waitDialogType);
				thread.execute();// 顺序执行
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);
		}
	}

	/**
	 * 发送POST请求
	 * 
	 * @param //paramList
	 *            请求参数
	 * @param ct
	 *            上下文对象
	 * @param url
	 *            请求地址
	 * @param waitDialogType
	 *            请求等待对话框类型 DialogUtils.NO_WAIT_DIALOG 不需要等待框
	 *            DialogUtils.WAIT_DIALOG 普通的等待框
	 */
	public void sendPostRequestImage(List<NameValuePair> params, Context ct,
			String url, int waitDialogType, String path) {
		LogUtils.print(ct, LogUtils.TYPE_CONSOLE, "saf",
				"网络状态：" + appBean.isNetStat() + "");

		if (appBean.isNetStat()) {// 网络状态正常
			if (!appBean.hs.containsKey(url)) {
				BaseThreadImage thread = new BaseThreadImage(params, ct, url,
						waitDialogType, path);
				thread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);
		}
	}

	/**
	 * 发送POST请求
	 * 
	 * @param paramList
	 *            请求参数
	 * @param ct
	 *            上下文对象
	 * @param url
	 *            请求地址
	 * @param waitDialogType
	 *            请求等待对话框类型 DialogUtils.NO_WAIT_DIALOG 不需要等待框
	 *            DialogUtils.WAIT_DIALOG 普通的等待框
	 */
	public void sendPostRequest(List<NameValuePair> paramList, String txnType,
			Context ct, String url, int waitDialogType) {
		LogUtils.print(ct, LogUtils.TYPE_CONSOLE, "saf",
				"网络状态：" + appBean.isNetStat() + "");

		if (appBean.isNetStat()) {// 网络状态正常
			if (!appBean.hs.containsKey(url)) {
				BaseThread thread = new BaseThread(paramList, ct, url,
						waitDialogType);
				thread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);
		}
	}

	/**
	 * 获取小文件请求
	 * 
	 * @param ct
	 * @param url
	 * @param waitDialogType
	 */

	public void getByte(Context ct, String url, int waitDialogType) {
		if (appBean.isNetStat()) {// 网络状态正常
			if (!appBean.hs.containsKey(url)) {
				GetByteThread getByteThread = new GetByteThread(ct, url,
						waitDialogType);
				getByteThread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);

		}

	}

	/**
	 * 获取大文件方法用户保存大文件到sd卡的相应目录
	 * 
	 * @param ct
	 * @param url
	 * @param direct
	 *            指定目录
	 * @param waitDialogType
	 */

	public void getBigFile(Context ct, String url, String direct,
			String fileName, int waitDialogType) {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Toast.makeText(mCt, "sd卡不存在", Toast.LENGTH_SHORT).show();
			return;// 如果sd卡没挂载取消联网
		}
		if (appBean.isNetStat()) {// 网络状态正常G
			if (!appBean.hs.containsKey(url)) {
				GetBigFileThread getBigFileThread = new GetBigFileThread(ct,
						url, direct, fileName, DialogUtils.NO_WAIT_DIALOG);
				getBigFileThread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);
		}

	}

	/**
	 * 获取大文件方法用户保存大文件到sd卡的相应目录(带进度条)
	 * 
	 * @param ct
	 * @param url
	 * @param direct
	 *            指定目录
	 * @param //waitDialogType
	 */

	public void getBigFile(Context ct, String url, String direct,
			String fileName, Handler handler) {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Toast.makeText(mCt, "sd卡不存在", Toast.LENGTH_SHORT).show();
			return;// 如果sd卡没挂载取消联网
		}
		if (appBean.isNetStat()) {// 网络状态正常G
			if (!appBean.hs.containsKey(url)) {
				GetBigFileThread getBigFileThread = new GetBigFileThread(ct,
						url, direct, fileName, handler,
						DialogUtils.NO_WAIT_DIALOG);
				getBigFileThread.execute();
			}
		} else {// 网络异常
			DialogUtils.showNetworkSetDialog(ct);

		}

	}

	/**
	 * 发送POST请求
	 * 
	 * @param paramList
	 *            请求参数
	 * @param ct
	 *            上下文对象
	 * @param url
	 *            请求地址
	 * @param waitDialogType
	 *            请求等待对话框类型 DialogUtils.NO_WAIT_DIALOG 不需要等待框
	 *            DialogUtils.WAIT_DIALOG 普通的等待框
	 */
	// ahfdksjdfkajfkajsdkf
	public void sendPostRequest(List<NameValuePair> paramList, Context ct,
			String url, int waitDialogType, long delay) {
		if (!appBean.hs.containsKey(url)) {
			BaseThreadDelay thread = new BaseThreadDelay(paramList, ct, url,
					waitDialogType, delay);
			thread.execute();
		}
	}

	/**
	 * 共用后台请求线程
	 * 
	 * @author zhb
	 * 
	 */
	private class BaseThread extends AsyncTask<Integer, Integer, Integer> {
		private Context mct;

		ICallbackListener iCallback; // 回调接口
		List<NameValuePair> mParamList;
		private String url;
		private int mWaitDialogType;// 请求等待对话框类型
		private ConnMachine commMachine;// 联网器
		String recvStr = null;

		/**
		 * 
		 * @param paramList
		 *            //请求参数
		 * @param ct
		 *            //上下文
		 * @param url
		 *            //请求地址
		 * @param waitDialogType
		 *            //请求等待对话框类型
		 */
		public BaseThread(List<NameValuePair> paramList, Context ct,
				String url, int waitDialogType) {
			iCallback = (ICallbackListener) ct;
			this.mParamList = paramList;
			this.mct = ct;
			this.url = url;
			this.mWaitDialogType = waitDialogType;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			proExecute(this, mWaitDialogType, url);// 开启请求等待框，同时加入等待框取消后的请求关闭操作
			commMachine = appBean.getCommMachine();// 获取联网器
			appBean.putUrl(url, commMachine);// 将url加入到hashmap

		}

		@Override
		protected Integer doInBackground(Integer... params) {

			try {
				// 发起请求
				if (commMachine.isCancel) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				}
				recvStr = commMachine.requestPost(mParamList, url);
			} catch (Exception e1) {
				e1.printStackTrace();
				if (commMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}
			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}
			return NetworkConstant.RIGHT_CODE;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}
			LogUtils.print(mct, LogUtils.TYPE_CONSOLE, "saf", recvStr);
			// 回调刷新主界面
			iCallback.callback(result, url, recvStr);

		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

	}

	/**
	 * 共用后台请求线程
	 * 
	 * @author zhb
	 * 
	 */
	private class BaseJsonThread extends AsyncTask<Integer, Integer, Integer> {
		private Context mct;

		ICallbackListener iCallback; // 回调接口
		Map<String, Object> requestMap;
		private String url;
		private int mWaitDialogType;// 请求等待对话框类型
		private ConnMachine commMachine;// 联网器
		String recvStr = null;

		/**
		 * 
		 * @param //paramList
		 *            //请求参数
		 * @param ct
		 *            //上下文
		 * @param url
		 *            //请求地址
		 * @param waitDialogType
		 *            //请求等待对话框类型
		 */
		public BaseJsonThread(Map<String, Object> map, Context ct, String url,
				int waitDialogType) {
			iCallback = (ICallbackListener) ct;
			this.requestMap = map;
			this.mct = ct;
			this.url = url;
			this.mWaitDialogType = waitDialogType;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			proExecute(this, mWaitDialogType, url);// 开启请求等待框，同时加入等待框取消后的请求关闭操作
			commMachine = appBean.getCommMachine();// 获取联网器
			appBean.putUrl(url, commMachine);// 将url加入到hashmap
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				// 发起请求
				if (commMachine.isCancel) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				}
				requestMap.put("phoneType", TransactionValues.phoneType);
				requestMap.put("userToken", TransactionValues.userToken);
				requestMap.put("pointX", TransactionValues.longitude);
				requestMap.put("pointY", TransactionValues.latitude);
				requestMap.put("cityCode", TransactionValues.cityCode);
//				String json = JsonUtils.createJsonString(requestMap);
//				LogUtils.print(mct, LogUtils.TYPE_CONSOLE, "MyNetwork----",
//						json);
//				int len=json.getBytes(NetworkConstant.ENCODE).length;
//				String  c=String.valueOf(len);
//				String cString="";
//				for(int i=0;i<8-c.length();i++){
//					cString += "0";
//				}
//				cString +=(c+json);
//				Logger.d("MyNetwork-----", "cString----="+cString);
				recvStr = commMachine.requestPostByMap(url, requestMap, null);
			} catch (Exception e1) {
				e1.printStackTrace();
				if (commMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}
			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}
			return NetworkConstant.RIGHT_CODE;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}
			LogUtils.print(mct, LogUtils.TYPE_CONSOLE, "saf-onPostExecute",
					recvStr);
			// 回调刷新主界面
			iCallback.callback(result, url, recvStr);

		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

	}

	/**
	 * 共用后台请求线程
	 * 
	 * @author zhb
	 * 
	 */
	private class BaseThreadImage extends AsyncTask<Integer, Integer, Integer> {
		private Context mct;

		ICallbackListener iCallback; // 回调接口
		List<NameValuePair> params ;
		private String url;
		private int mWaitDialogType;// 请求等待对话框类型
		private ConnMachine commMachine;// 联网器
		String recvStr = null;
		String path;

		/**
		 * 
		 * @param //paramList
		 *            //请求参数
		 * @param ct
		 *            //上下文
		 * @param url
		 *            //请求地址
		 * @param waitDialogType
		 *            //请求等待对话框类型
		 */
		public BaseThreadImage(List<NameValuePair> params, Context ct,
				String url, int waitDialogType, String path) {
			iCallback = (ICallbackListener) ct;
			this.params = params;
			this.mct = ct;
			this.url = url;
			this.mWaitDialogType = waitDialogType;
			this.path = path;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			proExecute(this, mWaitDialogType, url);// 开启请求等待框，同时加入等待框取消后的请求关闭操作
			commMachine = appBean.getCommMachine();// 获取联网器
			appBean.putUrl(url, commMachine);// 将url加入到hashmap

		}

		@Override
		protected Integer doInBackground(Integer... params) {

			try {
				// 发起请求
				if (commMachine.isCancel) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				}
				recvStr = commMachine.requestImage(url, this.params, path);
			} catch (Exception e1) {
				e1.printStackTrace();
				if (commMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}
			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}
			return NetworkConstant.RIGHT_CODE;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}
			LogUtils.print(mct, LogUtils.TYPE_CONSOLE, "saf", recvStr);
			// 回调刷新主界面
			iCallback.callback(result, url, recvStr);

		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

	}

	/**
	 * 获取byte数组数据的异步任务适合于小文件
	 * 
	 * @author wq
	 * @version 1.0
	 * @since 2013-1-23下午04:33:50
	 */
	private class GetByteThread extends AsyncTask<Integer, Integer, Integer> {
		@SuppressWarnings("unused")
		private Context mct;

		ICallbackListener iCallback; // 回调接口
		private String url;
		private int mWaitDialogType;// 请求等待对话框类型
		private ConnMachine commMachine;// 联网器
		byte[] arr = null;

		/**
		 * 
		 * @param //paramList
		 *            //请求参数
		 * @param ct
		 *            //上下文
		 * @param url
		 *            //请求地址
		 * @param waitDialogType
		 *            //请求等待对话框类型
		 */
		public GetByteThread(Context ct, String url, int waitDialogType) {
			iCallback = (ICallbackListener) ct;
			this.mct = ct;
			this.url = url;
			this.mWaitDialogType = waitDialogType;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			proExecute(this, mWaitDialogType, url);// 开启请求等待框，同时加入等待框取消后的请求关闭操作
			commMachine = appBean.getCommMachine();// 获取联网器
			appBean.putUrl(url, commMachine);// 将url加入到hashmap
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				// 发起请求
				if (commMachine.isCancel) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				}
				arr = commMachine.getBytes(url, appBean);
			} catch (Exception e1) {
				if (commMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}
			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}
			return 0;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}
			// 回调刷新主界面
			iCallback.callbackByte(result, url, arr);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

	}

	/*
	 * 请求大文件异步任务
	 */

	private class GetBigFileThread extends AsyncTask<Integer, Integer, Integer> {
		@SuppressWarnings("unused")
		private int mWaitDialogType;// 请求等待对话框类型
		@SuppressWarnings("unused")
		private Context mct;

		ICallbackListener iCallback; // 回调接口
		private String url;
		private String direct;
		private String fileName;
		private Handler handler;
		private ConnMachine commMachine;// 联网器
		int i = -1;// 获取文件的放回结果默认为失败

		/**
		 * 
		 * @param //paramList
		 *            请求参数
		 * @param ct
		 *            上下文
		 * @param url
		 *            请求地址
		 * @param direct
		 *            文件存放目录
		 * @param waitDialogType
		 *            请求等待对话框类型
		 */
		public GetBigFileThread(Context ct, String url, String direct,
				String fileName, int waitDialogType) {
			iCallback = (ICallbackListener) ct;
			this.mct = ct;
			this.url = url;
			this.direct = direct;
			this.fileName = fileName;
			this.mWaitDialogType = waitDialogType;
		}

		public GetBigFileThread(Context ct, String url, String direct,
				String fileName, Handler handler, int waitDialogType) {
			iCallback = (ICallbackListener) ct;
			this.fileName = fileName;
			this.mct = ct;
			this.url = url;
			this.direct = direct;
			this.fileName = fileName;
			this.handler = handler;
			this.mWaitDialogType = waitDialogType;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			commMachine = appBean.getCommMachine();// 获取联网器
			if (handler != null) {
				commMachine.setHandler(handler);
			}
			appBean.putUrl(url, commMachine);// 将url加入到hashmap
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				// 发起请求
				if (commMachine.isCancel) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				}
				i = commMachine.getRemoteFileInputStream(url, direct, fileName);
			} catch (Exception e1) {
				e1.printStackTrace();
				deletFromsd();// 出现异常了则尝试删除未下载完成的缺损文件
				if (commMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}
			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}

			return i;// 1为已经存在 2 为sd卡空间不足
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}

			// 回调刷新主界面
			iCallback.callbackBigFile(result, url);
		}

		// 从sd卡删除文件

		private void deletFromsd() {
			File file = new File(direct, fileName);
			if (file.exists()) {
				file.delete();

			}

		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

	}

	/**
	 * 共用后台请求线程
	 * 
	 * @author zhb
	 * 
	 */
	private class BaseThreadDelay extends AsyncTask<Integer, Integer, Integer> {
		ICallbackListener iCallback; // 回调接口
		List<NameValuePair> mParamList;
		private String url;
		private int mWaitDialogType;// 请求等待对话框类型
		private ConnMachine connMachine;
		String recvStr = null;
		long delay = 0;

		/**
		 * 
		 * @param paramList
		 *            //请求参数
		 * @param ct
		 *            //上下文
		 * @param url
		 *            //请求地址
		 * @param waitDialogType
		 *            //请求等待对话框类型
		 * @param delay
		 *            延迟时间
		 */
		public BaseThreadDelay(List<NameValuePair> paramList, Context ct,
				String url, int waitDialogType, long delay) {
			iCallback = (ICallbackListener) ct;
			this.mParamList = paramList;
			this.url = url;
			this.mWaitDialogType = waitDialogType;
			this.delay = delay;
		}

		/**
		 * 联网前准备连接器 存放url和machine到hashmap
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			proExecute(this, mWaitDialogType, url);
			connMachine = appBean.getCommMachine();// 获取联网器
			appBean.putUrl(url, connMachine);// 将url加入到hashmap
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				Thread.sleep(delay);
				// 发起请求
				recvStr = connMachine.requestPost(mParamList, url);
			} catch (Exception e1) {
				if (connMachine.isCancel || e1 instanceof CloseRequestException) {
					return NetworkConstant.FORCE_CLOSE;// 被强制关闭了
				} else if (e1 instanceof ConnectTimeoutException
						|| e1 instanceof SocketTimeoutException) {
					return NetworkConstant.TIMEOUT_ERROR;// 超时的异常
				} else {
					return NetworkConstant.OTHER_ERROR;// 其他异常
				}

			} finally {
				appBean.clearPoint(url);// 清理hashmap中url
			}
			return NetworkConstant.RIGHT_CODE;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// 取消等待对话框
			if (waitDialog != null && waitDialog.isShowing()) {
				waitDialog.cancel();
			}

			if (iCallback != null) {
				// 回调刷新主界面
				iCallback.callback(result, url, recvStr);
			}
		}
	}

	/**
	 * 关闭指定的联网请求
	 */
	public void close(String url) {
		ConnMachine connMachine = appBean.getMachine(url);
		if (connMachine != null) {
			connMachine.close();
		}
	}

	/**
	 * 销毁对话框
	 */
	public void dialogDestroy() {
		if (waitDialog != null && waitDialog.isShowing()) {
			waitDialog.cancel();
		}
	}

	/**
	 * 异步线程预备状态
	 * 
	 * @param task
	 *            当前异步线程对象
	 * @param mWaitDialogType
	 *            对话框类型
	 * @param //commMachine
	 *            联网器
	 * @param url
	 *            请求地址
	 */
	public void proExecute(final AsyncTask<?, ?, ?> task, int mWaitDialogType,
			final String url) {
		waitDialog = DialogUtils.showProgressDialog(mCt, mWaitDialogType,
				title, message);// 启动等待对话框
		if (waitDialog != null && waitDialog.isShowing()) {
			waitDialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					close(url);
					title = "请稍等！";
					message = "正在获取数据...";
				}
			});
		}
	}

}
